﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

using WeiboFilter.Models;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

namespace WeiboFilter.DAL
{//入数据： sina==>db
    //出数据：db==>本地页面
    public class UserDAL
    {
        private string sp_insertWeiBoUser = "dbo.insertWeiBoUser";
        private string sp_getWeiBoUserById = "dbo.getWeiBoUserById";

        public User InsertWeiBoUser(User t_u)
        {//当前账户如果已经在数据库中，则放弃
            User u = GetWeiBoUserById(t_u.id);
            if (u != null)//非空则返回
            {
                return u;
            }
            else
            {
                SqlParameter[] Params = new SqlParameter[]{
                    new SqlParameter("@id",t_u.id),
                new SqlParameter("@screen_name",t_u.screen_name),
                new SqlParameter("@description",t_u.description),
                new SqlParameter("@profile_image_url",t_u.profile_image_url)
                };
                int ret = SqlHelper.ExecuteNonQuery(SqlCommon.connString, CommandType.StoredProcedure, sp_insertWeiBoUser, Params);
                return t_u;
            }
        }

        public User GetWeiBoUserById(string id)
        {
            User u = new User();
            SqlParameter[] Params = new SqlParameter[]{
                new SqlParameter("@id",id)
            };
            using (SqlConnection conn = new SqlConnection(SqlCommon.connString))
            {
                using (SqlDataReader sdr = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, sp_getWeiBoUserById, Params))
                {
                    if (!sdr.HasRows)
                        return null;
                    sdr.Read();
                    u.id = sdr.GetString(0);
                    u.screen_name = sdr.GetString(1);
                    u.description = sdr.GetString(2);
                    u.profile_image_url = sdr.GetString(3);
                    u.domain = "http://t.sina.com.cn/" + u.id;
                }
            }
            return u;
        }

    }


}
